'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
  async tutorialcreate() {
    const { ctx } = this;
    const { uid, tocontent, tttheme, pictureurl } = ctx.request.body;
    if (!(uid && tocontent && tttheme && pictureurl)) {
      return ctx.body = { success: false, msg: '发布失败' };
    }
    try {
      const res = await ctx.model.Tutorial.create({
        uid,
        tttheme,
        tocontent,
        browse: 0,
        addTime: Date.now(),
      });
      const newurl = pictureurl.split(',');
      for (const item of newurl) {
        await ctx.model.Picture.create({
          tutorialid: res.tutorialid,
          photourl: item,
        });
      }
      ctx.body = { success: true, msg: '发布成功' };
    } catch (e) {
      ctx.body = { success: false, msg: '发布失败' };
    }

  }

  async tutorialfind() {
    const { ctx } = this;
    const { tutorialid, uid, tttheme, page } = ctx.request.body;
    const limit = 10;
    const offset = (page - 1) * limit || 0;
    const where = {};
    if (tutorialid) {
      where.tutorialid = tutorialid;
    }
    if (uid) {
      where.uid = uid;
    }
    if (tttheme) {
      where.tttheme = tttheme;
    }
    try {
      await ctx.model.Tutorial.hasMany(ctx.model.Picture, {
        foreignKey: 'tutorialid',
        sourceKey: 'tutorialid',
      });
      await ctx.model.Tutorial.hasMany(ctx.model.Thumbs, {
        foreignKey: 'tutorialid',
        sourceKey: 'tutorialid',
      });
      await ctx.model.Tutorial.hasMany(ctx.model.Comment, {
        foreignKey: 'tutorialid',
        sourceKey: 'tutorialid',
      });
      await ctx.model.Tutorial.hasMany(ctx.model.Collection, {
        foreignKey: 'tutorialid',
        sourceKey: 'tutorialid',
      });
      await ctx.model.Tutorial.hasOne(ctx.model.User, {
        foreignKey: 'uid',
        sourceKey: 'uid',
      });
      const res = await ctx.model.Tutorial.findAndCountAll({
        where,
        limit,
        offset,
        include: [{
          model: ctx.model.Picture,
        }, {
          model: ctx.model.Thumbs,
        }, {
          model: ctx.model.Collection,
        }, {
          model: ctx.model.Comment,
        }, {
          model: ctx.model.User,
          attributes: {
            exclude: [ 'pas' ],
          },
        }],
        order: [[ 'tutorialid', 'DESC' ]],
      });
      const length = await ctx.model.Tutorial.findAll({
        where,
      });
      ctx.body = { success: true, data: res, length: length.length, maxpage: Math.ceil(length.length / limit) };
    } catch (e) {
      ctx.body = { success: false, msg: '查询失败', data: e };
    }
  }

  async tutorialdelete() {
    const { ctx } = this;
    const { tutorialid } = ctx.request.body;
    if (!tutorialid) {
      return ctx.body = { success: false, msg: '请传入必要参数' };
    }
    try {
      await ctx.model.Tutorial.destroy({
        where: {
          tutorialid,
        },
      });
      ctx.body = { success: true, msg: '删除成功' };
    } catch (e) {
      ctx.body = { success: false, msg: '删除失败' };
    }
  }

  async tutorialupdate() {
    const { ctx } = this;
    const { tutorialid, tocontent, tttheme, browse } = ctx.request.body;
    if (!tutorialid) {
      return ctx.body = { success: false, msg: '请传入必要参数' };
    }
    const update = {};
    if (tocontent) {
      update.tocontent = tocontent;
    }
    if (tttheme) {
      update.tttheme = tttheme;
    }
    if (browse) {
      update.browse = browse;
    }
    try {
      await ctx.model.Tutorial.update(
        update,
        {
          where: {
            tutorialid,
          },
        }
      );
      ctx.body = { success: true, msg: '修改成功' };
    } catch (e) {
      ctx.body = { success: false, msg: '修改失败' };
    }
  }

  async tutorialike() {
    const { ctx, app } = this;
    const { Op } = app.Sequelize;
    const { search, page } = ctx.request.body;
    const limit = 10;
    const offset = (page - 1) * limit || 0;
    try {
      await ctx.model.Tutorial.hasMany(ctx.model.Picture, {
        foreignKey: 'tutorialid',
        sourceKey: 'tutorialid',
      });
      await ctx.model.Tutorial.hasMany(ctx.model.Thumbs, {
        foreignKey: 'tutorialid',
        sourceKey: 'tutorialid',
      });
      await ctx.model.Tutorial.hasMany(ctx.model.Comment, {
        foreignKey: 'tutorialid',
        sourceKey: 'tutorialid',
      });
      await ctx.model.Tutorial.hasMany(ctx.model.Collection, {
        foreignKey: 'tutorialid',
        sourceKey: 'tutorialid',
      });
      await ctx.model.Tutorial.hasOne(ctx.model.User, {
        foreignKey: 'uid',
        sourceKey: 'uid',
      });
      const res = await ctx.model.Tutorial.findAll({
        where: {
          [Op.or]: [
            {
              tttheme: {
                [Op.like]: `%${search}%`,
              },
            },
            {
              tocontent: {
                [Op.like]: `%${search}%`,
              },
            },
          ],
        },
        limit,
        offset,
        include: [{
          model: ctx.model.Picture,
        }, {
          model: ctx.model.Thumbs,
        }, {
          model: ctx.model.Collection,
        }, {
          model: ctx.model.Comment,
        }, {
          model: ctx.model.User,
          attributes: {
            exclude: [ 'pas' ],
          },
        }],
        order: [[ 'tutorialid', 'DESC' ]],
      });
      const length = await ctx.model.Tutorial.findAll({
        where: {
          [Op.or]: [
            {
              tttheme: {
                [Op.like]: `%${search}%`,
              },
            },
            {
              tocontent: {
                [Op.like]: `%${search}%`,
              },
            },
          ],
        },
      });
      ctx.body = { success: true, data: res, length: length.length, maxpage: Math.ceil(length.length / limit) };
    } catch (e) {
      ctx.body = { success: false, msg: '查询失败' };
    }
  }
}

module.exports = HomeController;
